#include "ImaServiceUtils.h"
#include "ImaLogger.h"
#include <stdio.h>
#include <uuid/uuid.h>
#include <string.h>

extern ImaLogger* gpLogger;

std::string ImaServiceUtils::generateTokenId(uint32_t ipAddress) {
	char log_msg[256];
	sprintf(log_msg, "generateToken called with ip %u", ipAddress);
	LOG(debug, __FILE__, __LINE__, log_msg);

	uuid_t uuid;
	uuid_generate(uuid);

	char strUuid[256];
	uuid_unparse(uuid, strUuid);

	sprintf(log_msg, "generated token id is %s", strUuid);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return std::string(strUuid);	
}

bool ImaServiceUtils::isValidEmailAddress(const char *pEmailAddress) {
   if(!pEmailAddress) // If cannot read the Email Address...
      return 0;
   if(!isCharacter(pEmailAddress[0])) // If the First character is not A-Z, a-z
      return 0;
   int AtOffset = -1;
   int DotOffset = -1;
   unsigned int Length = strlen(pEmailAddress); // Length = StringLength (strlen) of EmailAddress
   for(unsigned int i = 0; i < Length; i++) {
      if(pEmailAddress[i] == '@') // If one of the characters is @, store it's position in AtOffset
         AtOffset = (int)i;
      else if(pEmailAddress[i] == '.') // Same, but with the dot
         DotOffset = (int)i;
   }
   if(AtOffset == -1 || DotOffset == -1) // If cannot find a Dot or a @
      return 0;
   if(AtOffset > DotOffset) // If the @ is after the Dot
      return 0;
   return !(DotOffset >= ((int)Length-1)); //Chech there is some other letters after the Dot
}

